﻿// --------------------------------
// <copyright file="Externals.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:38</date>

// <summary>
//
// </summary>
// --------------------------------

#if WINDOWS

using sysdraw = System.Drawing;

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FITClub.Util
{
    /// <summary>
    /// Extensions for Point, Rectangle and Vector2
    /// </summary>
    public static class Extensions
    {
        public static Point Scale(this Point p, double deltaRatioX, double deltaRatioY)
        {
            return new Point((int)(p.X * deltaRatioX), (int)(p.Y * deltaRatioY));
        }

        public static Point Delta(this Point p, Point other)
        {
            return new Point(p.X - other.X, p.Y - other.Y);
        }

        public static Point DeltaPlus(this Point p, Point other)
        {
            return new Point(p.X + other.X, p.Y + other.Y);
        }

        public static Rectangle OffsetXY(this Rectangle rect, int deltaX)
        {
            return rect.OffsetXY(deltaX, 0);
        }

        public static Rectangle OffsetXY(this Rectangle rect, int deltaX, int deltaY)
        {
            return new Rectangle(rect.X + deltaX, rect.Y + deltaY, rect.Width, rect.Height);
        }

        public static Rectangle OffsetXY(this Rectangle rect, Vector2 delta)
        {
            return new Rectangle(rect.X + (int)delta.X, rect.Y + (int)delta.Y, rect.Width, rect.Height);
        }

        public static Rectangle ResetX(this Rectangle rect, int X)
        {
            return new Rectangle(X, rect.Y, rect.Width, rect.Height);
        }

        public static Rectangle ResetY(this Rectangle rect, int Y)
        {
            return new Rectangle(rect.X, Y, rect.Width, rect.Height);
        }

        public static Rectangle Centralize(this Rectangle rect, Rectangle parent)
        {
            return new Rectangle(parent.X + parent.Width / 2 - rect.Width / 2, parent.Y + parent.Height / 2 - rect.Height / 2, rect.Width, rect.Height);
        }

        public static Rectangle ScaleXY(this Rectangle rect, double deltaX)
        {
            return rect.ScaleXY(deltaX, 1);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="deltaX"></param>
        /// <param name="deltaY"></param>
        /// <returns></returns>
        public static Rectangle ScaleXY(this Rectangle rect, double deltaX, double deltaY)
        {
            return new Rectangle((int)(rect.X * deltaX), (int)(rect.Y * deltaY), (int)(rect.Width * deltaX), (int)(rect.Height * deltaY));
        }

        /// <summary>
        /// Determines that this Rectangle contains Vector2 as point.
        /// X and Y values are rounded.
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static bool ContainsRounded(this Rectangle rect, Vector2 position)
        {
            return rect.Contains(new Point((int)Math.Round(position.X, 0), (int)Math.Round(position.Y, 0)));
        }

        /// <summary>
        /// Determines that this Rectangle contains Vector2 as point.
        /// Decimal places of X and Y are cropped.
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static bool ContainsCropped(this Rectangle rect, Vector2 position)
        {
            return rect.Contains(new Point((int)position.X, (int)position.Y));
        }

        public static Vector2 LocationV2(this Rectangle rect)
        {
            return new Vector2(rect.X, rect.Y);
        }

        //// mozno bude stacit ratioY
        public static Vector2 CenterString(this SpriteFont font, string text, Rectangle parent, double ratioX, double ratioY)
        {
            Vector2 size = font.MeasureString(text);
            size.X = (float)Math.Ceiling(ratioX * size.X);
            size.Y = (float)Math.Ceiling(ratioY * size.Y);

            size.X = parent.X + parent.Width / 2 - size.X / 2;
            size.Y = parent.Y + parent.Height / 2 - size.Y / 2;

            return size;
        }

        public static bool Contains(this Vector4 rect, Vector2 point)
        {
            return rect.X >= point.X && rect.Y >= point.Y && rect.X + rect.Z <= point.X && rect.Y + rect.W <= point.Y;
        }

#if WINDOWS

        public static sysdraw.Color ToFormsColor(this Color xcolor)
        {
            return sysdraw.Color.FromArgb(xcolor.A, xcolor.R, xcolor.G, xcolor.B);
        }

        public static Color ToXnaColor(this sysdraw.Color color)
        {
            return new Color(color.R, color.G, color.B, color.A);
        }

#endif
    }
}